<template>
  <div class="dashboard-container" style="height: 100%">

    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="qrcode" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              二维码总量
            </div>
            <count-to :start-val="0" :end-val="qrcodeCount" :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="scan" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              扫码总量
            </div>
            <count-to :start-val="0" :end-val="scanCount" :duration="3000" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="shopping" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              订单总量
            </div>
            <count-to :start-val="0" :end-val="paymentCount" :duration="3200" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-shopping">
            <svg-icon icon-class="users" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              用户总量
            </div>
            <count-to :start-val="0" :end-val="userCount" :duration="3600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>

    <div style="margin-left: 20px; margin-top: 15px;">
      最近<el-select
        v-model="dayNum"
        placeholder="请选择"
        style="margin-left: 10px; margin-right: 10px; width: 80px;"
        @change="init"
      >
        <el-option
          v-for="item in 30"
          :key="item"
          :label="item"
          :value="item"/>
      </el-select>天
    </div>

    <div id="line_chart" style="height: 500px; margin-bottom: 20px; margin-left: auto; margin-right: auto;"/>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import CountTo from 'vue-count-to'
import paymentApi from '@/api/trace/payment'
import qrcodeApi from '@/api/trace/qrcode'
import userApi from '@/api/trace/user'
import scanApi from '@/api/trace/scan'

export default {
  name: '',
  components: {
    CountTo
  },
  data() {
    return {
      dayNum: 7,
      line_chart: '',
      pie_chart: '',
      userCount: '',
      paymentCount: '',
      qrcodeCount: '',
      scanCount: '',
      dateList: [],
      count0List: [],
      count1List: [],
      count2List: [],
      totalList: []
    }
  },
  // 调用
  mounted() {
    this.init()
  },
  methods: {
    init() {
      scanApi.info({ dayNum: this.dayNum }).then(response => {
        this.dateList = response.data.dateList
        this.count0List = response.data.count0List
        this.count1List = response.data.count1List
        this.count2List = response.data.count2List
        this.totalList = response.data.totalList
        this.$nextTick(function() {
          this.drawLine()
        })
      })
      paymentApi.count().then(res => {
        this.paymentCount = res.data.item
      })
      scanApi.count().then(res => {
        this.scanCount = res.data.item
      })
      userApi.count().then(res => {
        this.userCount = res.data.item
      })
      qrcodeApi.count().then(res => {
        this.qrcodeCount = res.data.item
      })
      // order.getCategoryRatio(dayNum)
      //   .then(response => {
      //     this.categoryRatio.names = response.data.names
      //     this.categoryRatio.payments = response.data.payments
      //     this.categoryRatio.total = response.data.total
      //     this.categoryRatio.seriesData = []
      //     for (let i = 0; i < this.categoryRatio.names.length; i++) {
      //       this.categoryRatio.seriesData.push({ 'value': this.categoryRatio.payments[i], 'name': this.categoryRatio.names[i] })
      //     }
      //     this.$nextTick(function() {
      //       this.drawPie()
      //     })
      //   })
    },
    drawLine() {
      this.line_chart = echarts.init(document.getElementById('line_chart'))
      this.line_chart.setOption({
        // tooltip: {
        //   trigger: 'axis'
        // },
        legend: {
          data: ['总扫描量', '正宗产品', '风险产品', '假冒产品']
        },
        // grid: {
        //   left: '3%',
        //   right: '4%',
        //   bottom: '3%',
        //   containLabel: true
        // },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.dateList
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          }
        },

        series: [
          {
            name: '总扫描量',
            type: 'line',
            smooth: true,
            data: this.totalList
          }, {
            name: '正宗产品',
            type: 'line',
            smooth: true,
            data: this.count0List
          }, {
            name: '风险产品',
            type: 'line',
            smooth: true,
            data: this.count1List
          }, {
            name: '假冒产品',
            type: 'line',
            smooth: true,
            data: this.count2List
          }
        ]
      })
    },
    drawPie() {
      // 基于准备好的dom，初始化echarts实例
      this.pie_chart = echarts.init(document.getElementById('pie_chart'))
      // 绘制图表
      this.pie_chart.setOption({
        title: {
          text: '近' + this.dayNum + '天用户购买的商品分类占比', // 主标题
          subtext: '近' + this.dayNum + '天总收入 ￥' + this.categoryRatio.total,
          x: 'center'// x轴方向对齐方式
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          bottom: 'bottom',
          data: this.categoryRatio.names
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: this.categoryRatio.seriesData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ],
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
.panel-group {
  margin-top: 18px;

.card-panel-col {
  margin-bottom: 32px;
}

.card-panel {
  height: 108px;
  cursor: pointer;
  font-size: 12px;
  position: relative;
  overflow: hidden;
  color: #666;
  background: #fff;
  box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
  border-color: rgba(0, 0, 0, .05);

&:hover {
.card-panel-icon-wrapper {
  color: #fff;
}

.icon-people {
  background: #40c9c6;
}

.icon-message {
  background: #36a3f7;
}

.icon-money {
  background: #f4516c;
}

.icon-shopping {
  background: #34bfa3
}
}

.icon-people {
  color: #40c9c6;
}

.icon-message {
  color: #36a3f7;
}

.icon-money {
  color: #f4516c;
}

.icon-shopping {
  color: #34bfa3
}

.card-panel-icon-wrapper {
  float: left;
  margin: 14px 0 0 14px;
  padding: 16px;
  transition: all 0.38s ease-out;
  border-radius: 6px;
}

.card-panel-icon {
  float: left;
  font-size: 48px;
}

.card-panel-description {
  float: right;
  font-weight: bold;
  margin: 26px;
  margin-left: 0px;

.card-panel-text {
  line-height: 18px;
  color: rgba(0, 0, 0, 0.45);
  font-size: 16px;
  margin-bottom: 12px;
}

.card-panel-num {
  font-size: 20px;
}
}
}
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

  .svg-icon {
    display: block;
    margin: 14px auto !important;
    float: none !important;
  }
}
}
</style>
